<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<script type="text/javascript">
	
	
		document.write("<br> ECMA_SCRIPT5: " + a);
		// FOR_EACH: iterates all over the array executing the function for each element	
		var a = [1,2,3,4,5];
		var sum = 0;
		document.write("<br> <br> 1) <b> forEach()</b>: ");
		document.write("<br> Array: " + a);
	
		a.forEach(function(value){
			sum += value; 
		});	
		document.write("<br> Sum: " + sum);
	
		//MAP: passes each element of the array to the specified function. It returns a new array, 
		//not like forEach that just modifies the existing one
		var b = [1,2,3];
		document.write("<br> <br> 2) <b> map(): </b> ");
		document.write("<br> Array: " + b);
	
		var c = b.map(function(x){			
			return x*x;
		});
		
		document.write("<br> Array(mapped): " + c);
		
		//FILTER: returns a subset of elements contained in the array
		var d = [1,2,3,4,5,6,7];
		document.write("<br> <br> 3) <b> filter(): </b> ");
		document.write("<br> Array: " + d);
		
		var e = d.filter(function(x){
			return x>4;
		});
		document.write("<br> Filter - elements > 4: [" + e + "]");
		
		//EVERY: same that all
		//SOME: same than "there exist". 
		var f = [1,2,3,4,5];
		document.write("<br> <br> 4) <b> some/any(): </b> ");
		document.write("<br> Array: " + f);
		
		var result = f.every(function(x){
			return x<10;
		});
		document.write("<br> Are all smaller than 10? " + result);
		
		result = f.some(isNaN);			
		document.write("<br> There is any non-number element? " + result);
		
		
		//REDUCE/REDUCE_RIGTH
		var g = [1,2,3];
		document.write("<br> <br> 3) <b> reduce(): </b> ");
		document.write("<br> Array: " + g);
		
		var reduction = g.reduce(function(x,y){
			return x+y;
		}, 0);
		
		document.write("<br> ReductionResult: " + reduction + " Array: " + g);
		
		//INDEX_OF LAST_INDEX_OF
		var h = [1,2,3, 1, 3, 100];
		document.write("<br> <br> 3) <b> indexOf(): </b> ");
		document.write("<br> Array: " + h);
		
		document.write("<br> Index of 1: " + h.indexOf(1));
		document.write("<br> Index of 3: " + h.indexOf(3) + " lastIndex of 3: " + h.lastIndexOf(3));		
		document.write("<br> Index of 100: " + h.indexOf(100));
		
		//IS_ARRAY
		var i = [1,2];
		var j = "thisIsAnString";
		document.write("<br> <br> 3) <b> isArray(): </b> ");
		document.write("<br> i: [" + i + "] j: " + j);
		
		document.write("<br> i isArray()?: " + Array.isArray(i));
		document.write("<br> j isArray()?: " + Array.isArray(j));
		
		
	</script>

</body>
</html>